package com.foreveross.crawl.common.core;

import java.io.Serializable;

import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/** 
 * @ClassName: ProxyChannelRelate 
 * @Description: TODO
 * @author luofangyi
 * @date 2014-10-15 下午5:33:35 
 *  
 */ 
@Entity
@Table(name = "PROXY_CHANNEL_RELATE")
@Cacheable
public class ProxyChannelRelateEntity extends DateIntervalEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7950891630037106088L;
	// Fields
	private ChannelInfoEntity channelInfo;
	private ProxyIpEntity proxyIp;
	private Long successNumber;
	private Long failNumber;
	
	@Override
	public String[] businessKeys() {
		return new String[]{this.getId().toString()};
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "PROXY_ID")
	public ProxyIpEntity getProxyIp() {
		return this.proxyIp;
	}

	public void setProxyIp(ProxyIpEntity proxyIp) {
		this.proxyIp = proxyIp;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "CHANNELINFO_ID")
	public ChannelInfoEntity getChannelInfo() {
		return this.channelInfo;
	}

	public void setChannelInfo(ChannelInfoEntity channelInfo) {
		this.channelInfo = channelInfo;
	}

	@Column(name = "FAIL_NUMBER")
	public Long getFailNumber() {
		return this.failNumber;
	}

	public void setFailNumber(Long failNumber) {
		this.failNumber = failNumber;
	}

	@Column(name = "SUCCESS_NUMBER")
	public Long getSuccessNumber() {
		return this.successNumber;
	}

	public void setSuccessNumber(Long successNumber) {
		this.successNumber = successNumber;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result
				+ ((channelInfo == null) ? 0 : channelInfo.hashCode());
		result = prime * result
				+ ((failNumber == null) ? 0 : failNumber.hashCode());
		result = prime * result + ((proxyIp == null) ? 0 : proxyIp.hashCode());
		result = prime * result
				+ ((successNumber == null) ? 0 : successNumber.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		ProxyChannelRelateEntity other = (ProxyChannelRelateEntity) obj;
		if (channelInfo == null) {
			if (other.channelInfo != null)
				return false;
		} else if (!channelInfo.equals(other.channelInfo))
			return false;
		if (failNumber == null) {
			if (other.failNumber != null)
				return false;
		} else if (!failNumber.equals(other.failNumber))
			return false;
		if (proxyIp == null) {
			if (other.proxyIp != null)
				return false;
		} else if (!proxyIp.equals(other.proxyIp))
			return false;
		if (successNumber == null) {
			if (other.successNumber != null)
				return false;
		} else if (!successNumber.equals(other.successNumber))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "ProxyChannelRelateEntity [channelInfo=" + channelInfo
				+ ", proxyIp=" + proxyIp + ", successNumber=" + successNumber
				+ ", failNumber=" + failNumber + "]";
	}

	public static void deleteAll(){
		getRepository().createJpqlQuery("delete from ProxyChannelRelateEntity").executeUpdate();
	}
	
	public static <T extends org.dayatang.domain.Entity> T load(Class<T> clazz, Serializable id) {
		return getRepository().load(clazz, id);
	}
	
	public static void saveOrUpdate(ProxyChannelRelateEntity entity){
		getRepository().save(entity);
	}
	
//	public static void updateProxyIpOfSuccessNumber(Long channelId, String proxyIp) {
//		String jpql = "update ProxyChannelRelateEntity t set t.successNumber = successNumber + 1 where t.channelInfo.channelId=:channelId and t.proxyIp.ip=:ip";
//		getRepository().createJpqlQuery(jpql).addParameter("channelId", channelId).addParameter("ip", proxyIp).executeUpdate();
//	}
	public static void updateProxyIpOfSuccessNumber(ChannelInfoEntity channelInfo, ProxyIpEntity proxyIp) {
		String jpql = "update ProxyChannelRelateEntity t set t.successNumber = successNumber + 1 where t.channelInfo=:channelInfo and t.proxyIp=:proxyIp";
		getRepository().createJpqlQuery(jpql).addParameter("channelInfo", channelInfo).addParameter("proxyIp", proxyIp).executeUpdate();
	}
	
//	public static void updateProxyIpOfFailNumber(Long channelId, String proxyIp) {
//		String jpql = "update ProxyChannelRelateEntity t set t.failNumber = successNumber + 1 where t.channelInfo.channelId=:channelId and t.proxyIp.ip=:ip";
//		getRepository().createJpqlQuery(jpql).addParameter("channelId", channelId).addParameter("ip", proxyIp).executeUpdate();
//	}
	public static void updateProxyIpOfFailNumber(ChannelInfoEntity channelInfo, ProxyIpEntity proxyIp) {
		String jpql = "update ProxyChannelRelateEntity t set t.failNumber = failNumber + 1, t.disableDate = sysdate() where t.channelInfo=:channelInfo and t.proxyIp=:proxyIp";
		getRepository().createJpqlQuery(jpql).addParameter("channelInfo", channelInfo).addParameter("proxyIp", proxyIp).executeUpdate();
	}
	
	public static void updateProxyIpOfSuccessNumber(ChannelInfoEntity channelInfo, ProxyIpEntity proxyIp, int number) {
		String jpql = "update ProxyChannelRelateEntity t set t.successNumber=:number, t.disableDate = sysdate() where t.channelInfo=:channelInfo and t.proxyIp=:proxyIp";
		getRepository().createJpqlQuery(jpql).addParameter("channelInfo", channelInfo).addParameter("proxyIp", proxyIp).addParameter("number", number).executeUpdate();
	}
	
//	public static void updateProxyIpOfFailNumber(Long channelId, String proxyIp, int number) {
//		String jpql = "update ProxyChannelRelateEntity t set t.failNumber=:number where t.channelInfo.channelId=:channelId and t.proxyIp.ip=:ip";
//		getRepository().createJpqlQuery(jpql).addParameter("channelId", channelId).addParameter("number", number).addParameter("ip", proxyIp).executeUpdate();
//	}
	public static void updateProxyIpOfFailNumber(ChannelInfoEntity channelInfo, ProxyIpEntity proxyIp, int number) {
		String jpql = "update ProxyChannelRelateEntity t set t.failNumber=:number where t.channelInfo=:channelInfo and t.proxyIp=:proxyIp";
		getRepository().createJpqlQuery(jpql).addParameter("channelInfo", channelInfo).addParameter("proxyIp", proxyIp).addParameter("number", number).executeUpdate();
	}
	
//	public static boolean isProxyChannelRelateExisted(Long channelId, String ip){
//		String jpql = "select t from ProxyChannelRelateEntity t where t.channelInfo.channelId=:channelId and t.proxyIp.ip=:ip";
//		return !getRepository().createJpqlQuery(jpql).addParameter("channelId", channelId).addParameter("ip", ip).list().isEmpty();
//	}
	
	public static boolean isProxyChannelRelateExisted(ChannelInfoEntity channelInfo, ProxyIpEntity proxyIp){
		String jpql = "select t from ProxyChannelRelateEntity t where t.channelInfo=:channelInfo and t.proxyIp=:proxyIp";
		return !getRepository().createJpqlQuery(jpql).addParameter("channelInfo", channelInfo).addParameter("proxyIp", proxyIp).list().isEmpty();
	}
}